
import java.awt.Component;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author NeverMore
 */
public class GetInfo {

    ConnectDB conecttion = new ConnectDB();
    Connection conn = conecttion.Connect();
    ResultSet rs = null;

    public void GetInfoComboBox(String produceName, JComboBox jcbo) {
        try {
            CallableStatement call = conn.prepareCall("{call (?)}");
            call.setString(1, produceName);
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                String sClassCode = rs.getString(1);
                jcbo.addItem(sClassCode);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void GetInfoComboBoxByName(String produceName, String objName, JComboBox jcbo) {
        try {
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                String sClassCode = rs.getString(1);
                jcbo.addItem(sClassCode);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void GetInfoLabel(String produceName, JLabel jlb) {
        try {
            CallableStatement call = conn.prepareCall("{call (?)}");
            call.setString(1, produceName);
            ResultSet rs = call.executeQuery();

            while (rs.next()) {
                jlb.setText(rs.getString(1));
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void GetInfoLabelByName(String produceName, String objName, JLabel jlb) {
        try {
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            ResultSet rs = call.executeQuery();

            while (rs.next()) {
                jlb.setText(rs.getString(1));
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void GetInfoTxtByName(String produceName, String objName, JTextField jtxt) {
        try {
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            rs = call.executeQuery();
            while (rs.next()) {
                String add = rs.getString(1);
                jtxt.setText(add);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String GetInfoString(String produceName, String objName) {
        try {
            CallableStatement call = conn.prepareCall("{call (?)}");
            call.setString(1, produceName);
            rs = call.executeQuery();
            while (rs.next()) {
                objName = rs.getString(1);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objName;
    }

    public String GetInfoStringByName(String produceName, String objName, String obj) {
        try {
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            rs = call.executeQuery();
            while (rs.next()) {
                obj = rs.getString(1);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public void GetInfoTable( String produceName,JTable jtbl) {
        DefaultTableModel model;
        int j = 0;
        String mang[];
        try {
            model = (DefaultTableModel) jtbl.getModel();
            model.setRowCount(0);
            CallableStatement call = conn.prepareCall("{call (?)}");
            call.setString(1, produceName);
            ResultSet rs = call.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int NumOfCol = rsmd.getColumnCount();
            mang = new String[NumOfCol];
            for (int i = 1; i <= NumOfCol; i++) {
                mang[j] = rsmd.getColumnName(i);
                j++;
            }
            while (rs.next()) {
                Vector v = new Vector();
                for (int i = 0; i < mang.length; i++) {
                    v.addElement(rs.getString(mang[i]));
                }
                model.addRow(v);
            }
            rs.close();
            call.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void GetInfoTableByName(String produceName,String objName,JTable tblBillCom ) {
        Connection conn = conecttion.Connect();
        DefaultTableModel model;
        int j = 0;
        String mang[];
        try {
            model = (DefaultTableModel) tblBillCom.getModel();
            model.setRowCount(0);
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            ResultSet rs = call.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int NumOfCol = rsmd.getColumnCount();
            mang = new String[NumOfCol];
            for (int i = 1; i <= NumOfCol; i++) {
                mang[j] = rsmd.getColumnName(i);
                j++;
            }
            while (rs.next()) {
                Vector v = new Vector();
                for (int i = 0; i < mang.length; i++) {
                    v.addElement(rs.getString(mang[i]));
                }
                model.addRow(v);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void GetInfoSearchByName(String produceName,String objName,JTable tblBillCom) {
        Connection conn = conecttion.Connect();
        DefaultTableModel model;
        
        int j = 0;
        String mang[],col[]={""};
        try {
            model = (DefaultTableModel) tblBillCom.getModel();
            model.setRowCount(0);
            model.setColumnIdentifiers(col);
            CallableStatement call = conn.prepareCall("{call ?(?)}");
            call.setString(1, produceName);
            call.setString(2, objName);
            ResultSet rs = call.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int NumOfCol = rsmd.getColumnCount();
            mang = new String[NumOfCol];
            for (int i = 1; i <= NumOfCol; i++) {
                mang[j] = rsmd.getColumnName(i);
                j++;
            }
            model.setColumnIdentifiers(mang);
            while (rs.next()) {
                Vector v = new Vector();
                for (int i = 0; i < mang.length; i++) {
                    v.addElement(rs.getString(mang[i]));
                }
                model.addRow(v);
            }
            rs.close();
            call.close();
        } catch (Exception e) {
            
        }
    }
}
